//
//  YXLabelView.m
//  NewYiQiJun
//
//  Created by 张笙 on 2017/8/23.
//  Copyright © 2017年 李英新. All rights reserved.
//

#import "YXLabelView.h"

static int ButT = 6;//按钮距离上边
static int ButL = 12;//按钮距离左边



@interface YXLabelView ()

@property (nonatomic,assign) CGFloat ViewW;

@end

@implementation YXLabelView

-(instancetype) initWithFrame:(CGRect)frame dataArr:(NSArray *)array selectBtn:(NSUInteger)BtnTag{
    
    if (self = [super initWithFrame:frame]) {
        
        self.ViewW = frame.size.width;
        self.count = 0;
        
        for (int i = 0; i < array.count; i ++)
        {
            
            NSString *name = array[i];
            
            static UIButton * recordBtn = nil;
            
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn setTitle:[NSString stringWithFormat:@" · %@",name] forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:14.0];
            [btn setTitleColor:[UIColor colorWithRed:180/255.0 green:180/255.0 blue:180/255.0 alpha:1.0] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
            
            CGRect rect = [name boundingRectWithSize:CGSizeMake(self.bounds.size.width, 30) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:btn.titleLabel.font} context:nil];
            
            CGFloat BtnW = rect.size.width + 2*ButL;
            CGFloat BtnH = rect.size.height + 2* ButT;
            btn.layer.masksToBounds = YES;
            btn.layer.cornerRadius = 5;
            btn.backgroundColor = [UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1.0];
            if (i == 0){
                btn.frame =CGRectMake(ButL, ButT, BtnW, BtnH);
            }else{
                
                if (CGRectGetMaxX(recordBtn.frame)+ButL+BtnW > KScreenW) {
                    btn.frame = CGRectMake(ButL, CGRectGetMaxY(recordBtn.frame)+ButT, BtnW, BtnH);
                    
                }else{
                    btn.frame = CGRectMake(CGRectGetMaxX(recordBtn.frame) + ButL, recordBtn.frame.origin.y, BtnW, BtnH);
                }
            }
            
            if (i == BtnTag) {
                btn.selected = YES;
                btn.backgroundColor = [UIColor colorWithRed:106/255.0 green:179/255.0 blue:248/255.0 alpha:1];
            }
            
            [self addSubview:btn];
            self.frame = CGRectMake(0, 0, self.ViewW,CGRectGetMaxY(btn.frame)+10);
            
            recordBtn = btn;
            
            btn.tag = 100 + i;
            
            [btn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
            
            
        }
    }
    return self;
}

-(void)BtnClick:(UIButton *)sender{
    
    if ([self.LabelType isEqual:@"more"]) {
        if (sender.selected) {
            NSLog(@"取消");
            sender.backgroundColor = [UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1];
            [self.delegate ClickOnLabelOfNumb:sender.tag andTypr:sender.selected];
            self.count --;
        }else{
            
            NSLog(@"选中");
            if (self.count > 1000) {
                return;
            }else{
                
                sender.backgroundColor = [UIColor colorWithRed:106/255.0 green:179/255.0 blue:248/255.0 alpha:1];
                [self.delegate ClickOnLabelOfNumb:sender.tag andTypr:sender.selected];
                self.count ++;
            }
        }
        sender.selected = !sender.selected;
    }else{
        
        for (int i = 0; i < self.subviews.count; i ++) {
            
            UIButton * button = (UIButton *)[self viewWithTag:i+ 100];
            if (sender.tag == button.tag) {
                button.selected = YES;
                button.backgroundColor = [UIColor colorWithRed:106/255.0 green:179/255.0 blue:248/255.0 alpha:1];
                
            }else{
                button.selected = NO;
                button.backgroundColor = [UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1];
            }
            
        }
        [self.delegate ClickOnLabelOfNumb:sender.tag andTypr:sender.selected];
    }
}

@end
